package com.magicborrow.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.magicborrow.R;
import com.magicborrow.beans.SchoolEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ronaldo on 2016/6/9.
 */
public class  SchoolAdapter extends RecyclerView.Adapter{

    private List<SchoolEntity.SchoolData> data=new ArrayList<>();

    public void setData(List<SchoolEntity.SchoolData> data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(parent.getContext(), R.layout.item_school, null);
            return new SchoolViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SchoolViewHolder){
            ((SchoolViewHolder) holder).tvSchool.setText(data.get(position).getName());
            holder.itemView.setOnClickListener(new MyClickListener(position));
        }
    }


    class MyClickListener implements View.OnClickListener{
        private int mPosition;
        public MyClickListener(int position){
            this.mPosition=position;
        }

        @Override
        public void onClick(View view) {
            if(listener!=null){
                listener.onClickSchool(data.get(mPosition).getName(),data.get(mPosition).getId());
            }
        }
    }



    private OnClickSchoolListener listener;

    public void setOnClickSchoolListener(OnClickSchoolListener listener){
        this.listener=listener;
    }

    public interface OnClickSchoolListener{
        void onClickSchool(String selText,long selId);
    }



    @Override
    public int getItemCount() {
        return data.size();
    }

    private class SchoolViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvSchool;

        public SchoolViewHolder(View view) {
            super(view);
            tvSchool = (TextView) view.findViewById(R.id.tv_school);
        }
    }

//    private class FootViewHolder extends RecyclerView.ViewHolder {
//        public FootViewHolder(View view) {
//            super(view);
//        }
//    }
}
